/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.file;

import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import it.francescogabbrielli.android.solidalapp.Constants;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.StatusEvent;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.profileendpoint.model.Profile;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;

public class ResponseFileFragment extends FileFragment implements LoaderCallbacks<Profile> {

	public ResponseFileFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_file_response, container, false);
	}
	
	@Override
	public void onStatusUpdate(StatusEvent event) {
	
	}
	
	@Override
	public void onDataChanged(DataEvent event) {
		
		Request request = (Request) app.getData(DataKey.FILE_REQUEST);
		Profile profile = (Profile) app.getData(DataKey.FILE_REQUEST_PROFILE);
		
		if (request==null)
			return;
		
		switch (event.getKey()) {
	
		case FILE_REQUEST:
			Bundle filters = new Bundle();
			filters.putLong("id", request.getSenderId());
			LoaderManager lm = getActivity().getSupportLoaderManager();
			if (event.isTriggered())
				lm.restartLoader(Constants.LOADER_FILE_REQUEST_PROFILE, filters, this);
			else
				lm.initLoader(Constants.LOADER_FILE_REQUEST_PROFILE, filters, this);
			break;
			
		case FILE_REQUEST_PROFILE:
			updateProfile(profile, request.getMessage(), request.getTimestamp());
			break;
		
		default:
			break;
		}
	}
	
	@Override
	public Loader<Profile> onCreateLoader(int id, Bundle data) {
		Request res = (Request) app.getData(DataKey.FILE_REQUEST);
		return newLoader(res!=null ? res.getRegistrationId() : null)
				.setFilters(data);
	}
	
	@Override
	public void onLoadFinished(Loader<Profile> loader, Profile result) {
		app.setData(DataKey.FILE_REQUEST_PROFILE, result);
	}
	
	@Override
	public void onLoaderReset(Loader<Profile> loader) {
	
	}

}
